﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public static class DB
    {
        public static SqlConnection conexion()
        {
            string conexionString = "Data Source=JO_NAZAR;Initial Catalog=GIMNASIO;Integrated Security=True";
            SqlConnection con = new SqlConnection(conexionString);
            con.Open();
            return con;
        }
        public static SqlCommand ArmaCommand(string strSQL, List<SqlParameter> param)
        {
            
            SqlCommand com = new SqlCommand(strSQL);
            
            com.CommandType = CommandType.Text;
            foreach (SqlParameter sp in param)
            {
                com.Parameters.Add(sp);
            }
            return com;
        }
        public static SqlDataReader GenerarReader(string strSQL, List<SqlParameter> param)
        {
            SqlCommand com = ArmaCommand(strSQL, param);
            SqlConnection con = new SqlConnection();
            con = conexion();
            com.Connection = con;
            return com.ExecuteReader();
        }

        public static SqlDataReader GenerarReader(string strSQL)
        {
            SqlCommand com = new SqlCommand(strSQL);
            SqlConnection con = new SqlConnection();
            con = conexion();
            com.Connection = con;
            return com.ExecuteReader();
        }

        /*public static DataTable GenerarTable(String strSQL, List<SqlParameter> lista, SqlConnection con)
        {
            DataTable dt = new DataTable();
            dt.Load(GenerarReader(strSQL, lista, con));
            return dt;
        }*/

        public static int EjecutarCommand(String strSQL, List<SqlParameter> lista)
        {
            int i=0;
            SqlCommand com = new SqlCommand();
            SqlConnection con = new SqlConnection();
            try
            {
                com = ArmaCommand(strSQL, lista);
                con = conexion();
                com.Connection = con;
                i = com.ExecuteNonQuery();
                
            }
            catch (SystemException e)
            {
                e.Message.ToString();
            }
            finally
            {
                con.Close();
            }

            return i;
            
        }
        public static int EjecutarCommand(String strSQL, List<SqlParameter> lista, SqlTransaction tran)
        {
            SqlCommand com = ArmaCommand(strSQL, lista);
            com.Transaction = tran;
            return com.ExecuteNonQuery();
        }
        

    }
}
